package jp.example.sinya.fragment.manage.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import jp.example.sinya.fragment.manage.OnChangeFragmentListener;
import jp.example.sinya.fragment.manage.R;

/**
 * @author Sinya
 * @editor
 * @date 23/4/2018 6:17 PM
 * @describe
 */
public abstract class BaseFragment extends Fragment {

    protected TextView tvFragmentName;

    protected Button btnBackLast;
    protected Button btnBackMain;
    protected Button btnOpenNext;

    protected OnChangeFragmentListener onChangeFragmentListener;

    public void setOnChangeFragmentListener(OnChangeFragmentListener onChangeFragmentListener) {
        this.onChangeFragmentListener = onChangeFragmentListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayout(), container, false);
        setFragmentName(view);
        setClickListener(view);
        initUI();
        return view;
    }

    private int getLayout() {
        return R.layout.fragment_blank;
    }

    private void setFragmentName(View view) {
        if (view != null) {
            tvFragmentName = view.findViewById(R.id.tv_fragment_name);
            if (tvFragmentName != null) {
                tvFragmentName.setText("This is " + getClass().getSimpleName());
            }
        }
    }

    private void setClickListener(View view) {
        if (view != null) {
            btnBackLast = view.findViewById(R.id.btn_back_last);
            btnBackMain = view.findViewById(R.id.btn_back_main);
            btnOpenNext = view.findViewById(R.id.btn_open_next);


            if (btnBackLast != null) {
                btnBackLast.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        backLastFragment();
                    }
                });
            }

            if (btnBackMain != null) {
                btnBackMain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        backMainFragment();
                    }
                });
            }

            if (btnOpenNext != null) {
                btnOpenNext.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openNextFragment();
                    }
                });
            }
        }
    }

    protected abstract void initUI();

    protected void backLastFragment() {
        if (onChangeFragmentListener != null) {
            onChangeFragmentListener.backLastFragment();
        }
    }

    protected void backMainFragment() {
        if (onChangeFragmentListener != null) {
            onChangeFragmentListener.backMainFragment();
        }
    }

    protected abstract void openNextFragment();


}
